
from neo4j_db.region.region import (
    getAllRegionNodes,
    getRegionNodeRelationships,
    getRegionNodeByName
)
from utils.convert import (
    convertNode,
    convertRelationshipsToNodeAndRelationship
)
from utils.wrapData import (
    wrapFailure,
    wrapSuccess,
    wrapRegionDetailData
)
from utils.error import (
    ERROR_NODE_NOT_FOUND
)
from neo4j_db.nodeType import (
    HERO_NODE_TYPE
)
# 获取所有的区域节点
def getAllRegionNodesData():
    nodes = getAllRegionNodes()
    if nodes == None:
        return wrapFailure(**ERROR_NODE_NOT_FOUND)
    data = []
    for node in nodes:
        node_data = convertNode(node)
        data.append(node_data)
    return wrapSuccess(data)

# 根据某个节点获取相关的节点内容
def getRegionNodeRelationshipsData(name):
    node = getRegionNodeByName(name = name)
    if node == None:
        return wrapFailure(**ERROR_NODE_NOT_FOUND)
    relationships = getRegionNodeRelationships(node)
    node_data = convertNode(node)
    relationships_data = convertRelationshipsToNodeAndRelationship(relationships)
    # 这里主要把英雄的节点提取出来，这样的话提供给前端会更方便一些
    heros = []
    for relationship in relationships_data:
        node = relationship['start_node']
        # 根据节点类型——英雄节点类型提取
        # 由于存入neo4j数据之后，label会在字符串前加个冒号，因此我们比较的时候也该加上这个冒号
        if node['label'] == ':' + HERO_NODE_TYPE:
            heros.append(node)
    result = wrapRegionDetailData(heros = heros, node = node_data, relationships = relationships_data)
    return wrapSuccess(result)
